/*   点击左边频道列表    */
function clickChannel(id,type,nid){
  $("#divLoadingList").show();
  replaceHtml("divList", '');
  var data = {
    join_id:type,
    id:id,
    uid:uid,
    nid:nid
  };
  //debug(data);
  ajax.send(url+"/ajaxClickChannel",data,function(obj){
    //debug(obj);
    if (obj.status != 1) {
      alert("error");
      window.location.href = url;
    }
    var data = obj.data;
    replaceHtml("divList", _getHTMLTitle(data));
    $("#divLoadingList").hide();
  });
}

/*  画顶部列表  */
function _getHTMLTitle(data){
  var str = [], p = 0;
  if (data == null) return "";
  str[p++] = '<table class="grid">';
  for (var i = 0; i < data.length; i++) {
    var d = data[i];
    str[p++] = '<tr><td width="700px"><a href="javascript:void(0)" onclick="clickTitle('+d.id+')">'+d.title+'</a></td>';
    str[p++] = '<td width="200px">'+d.username+'</td>';
    str[p++] = '<td>'+d.updatetime+'</td>';
    str[p++] = '</tr>';
  }
  str[p++] = '</table>';
  return str.join('');
}

/*   点击顶部的列表中的标题，改变下部的文章内容   */
function clickTitle(id){
  $("#divLoadingArticle").show();
  replaceHtml("article", '');
  var data = {
    nid:nid,
    id:id,
    uid:uid
  };
  //debug(data);
  ajax.send(url+"/ajaxClickTitle",data,function(obj){
    //debug(obj);
    if (obj.status != 1) {
      alert("error");
      window.location.href = url;
    }
    var data = obj.data;
    if (data) {
      cinfo['id'] = data.id;
      cinfo['title'] = data.title;
      replaceHtml("article", _getHTMLArticle(data));
    }
    $("#divLoadingArticle").hide();
  });
}

/*  画文章内容  */
function _getHTMLArticle(data){
  var str = [], p = 0;
  if (data == null) return "";
  if (nid != 3) str[p++] = '<div class="article-title"><h2>'+data.title+'</h2><div class="article-info"><span>来源：中国报 </span><span>发布：能源</span><span>日期：'+data.updatetime+'</span>';
  str[p++] = '</div></div><div class="article-cont" align="center">';
  if (nid == 3) {
    str[p++] = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="800" height="600">\n\
<param name="movie" value="http://www.ehs.cn/admin_swf/'+data.content+'.swf" />\n\
<param name="quality" value="high" />\n\
<embed src="http://www.ehs.cn/admin_swf/'+data.content+'.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="800" height="600" >\n\
</embed></object>';
  }else str[p++] = data.content;
  str[p++] = '';
  str[p++] = '</div>'
  return str.join('');
}

/*  点击收藏夹中文章的标题，暂时为了低耦合性，分开写，将来可以考虑合并到clickTitle中去  */
function clickFavoriteTitle(type,tid){
  clickTitle(tid);
}

/*  双击然后重名题收藏夹名字或者文章记录的名字  */
function dbclickChangeName(self){
  $(self).hide();
  $(self).next().show();
}

/*  跟上面函数配套使用  */
function changeInputValue(self,id,type){
  var table = {
    1:'a_favorite',
    2:'a_favorite_list'
  }
  $(self).hide();
  var val = $(self).val();
  $(self).prev().text(val).show();
  var data = {
    id:id,
    title:val,
    table:table[type],
    type:nid
  };
  ajax.send(url+"/ajaxSaveFavoriteName",data,function(obj){
    initCollect(obj.data);
  });
}

//点击新建文件夹功能，返回后更新收藏夹列表divFavList和收藏点击按钮的弹出界面mm
function clickNewFavFile(did,level){
  var str = window.prompt("请输文件夹名称","新建文件夹")
  if (str == null) return ;
  if (str == "") {
    alert('输入的文件夹名称不能为空');
    return ;
  }
  var data = {
    uid:uid,
    type:nid,
    did:did,
    title:str,
    level:level
  };
  ajax.send(url+"/ajaxSaveNewFavFile",data,function(obj){
    initCollect(obj.data);
  })
}

/*  画收藏夹列表  */
function _getHTMLFavList(data){
  var str = [], p = 0;
  str[p++] = '<ul id="tt1">';
  str[p++] = '<li><div class="tree-node"><span class="tree-folder tree-folder-open icon-star4"></span><span class="tree-title" oncontextmenu="rightclickMenu(event,this,0,1,1)">我的收藏夹</span></div></li>';
  for (var d in data) {
    var da = data[d];
    str[p++] = '<li>';
    str[p++] = '<span><a href="javascript:void(0)" ondblclick="dbclickChangeName(this)" oncontextmenu="rightclickMenu(event,this,'+da.id+',1,2)">'+da.title+'</a><input type="text" value="'+da.title+'" style="display:none" onblur="changeInputValue(this,'+da.id+',1)"/></span>';
    str[p++] = '<ul>';
    for (var i in da.downLine) {
      var dd = da.downLine[i];
      str[p++] = '<li>';
      str[p++] = '<span><a href="javascript:void(0)" ondblclick="dbclickChangeName(this)" oncontextmenu="rightclickMenu(event,this,'+dd.id+',1,3)">'+dd.title+'</a><input type="text" value="'+dd.title+'" style="display:none" onblur="changeInputValue(this,'+dd.id+',1)"/></span>';
      str[p++] = '<ul>';
      for (var z in dd.downLine) {
        var ddz = dd.downLine[z];
        str[p++] = '<li>';
        str[p++] = '<span><a href="javascript:void(0)" ondblclick="dbclickChangeName(this)" oncontextmenu="rightclickMenu(event,this,'+ddz.id+',1,4)">'+ddz.title+'</a><input type="text" value="'+ddz.title+'" style="display:none" onblur="changeInputValue(this,'+ddz.id+',1)"/></span>';
        str[p++] = '<ul>';
        for (var s = 0; s < ddz._list.length; s++) {
          var dds = ddz._list[s];
          str[p++] = '<li><span>';
          str[p++] = '<a href="javascript:void(0)" onclick="clickFavoriteTitle('+dds.type+','+dds.tid+')" ondblclick="dbclickChangeName(this)" oncontextmenu="rightclickMenu(event,this,'+dds.id+',2,9)">'+dds.title+'</a>';
          str[p++] = '<input type="text" value="'+dds.title+'" style="display:none" onblur="changeInputValue(this,'+dds.id+',2)"/>';
          str[p++] = '</span></li>';
        }
        str[p++] = '</ul>';
        str[p++] = '</li>';
      }
      str[p++] = '</ul>';
      str[p++] = '</li>';
      for (var j = 0; j < dd._list.length; j++) {
        var ddj = dd._list[j];
        str[p++] = '<li><span>';
        str[p++] = '<a href="javascript:void(0)" onclick="clickFavoriteTitle('+ddj.type+','+ddj.tid+')" ondblclick="dbclickChangeName(this)" oncontextmenu="rightclickMenu(event,this,'+ddj.id+',2,9)">'+ddj.title+'</a>';
        str[p++] = '<input type="text" value="'+ddj.title+'" style="display:none" onblur="changeInputValue(this,'+ddj.id+',2)"/>';
        str[p++] = '</span></lid>';
      }
    }
    str[p++] = '</ul>';
    str[p++] = '</li>';
    for (var k = 0; k < da._list.length; k++) {
      var l = da._list[k];
      str[p++] = '<li><span>';
      str[p++] = '<a href="javascript:void(0)" onclick="clickFavoriteTitle('+nid+','+l.tid+')" ondblclick="dbclickChangeName(this)" oncontextmenu="rightclickMenu(event,this,'+l.id+',2,9)">'+l.title+'</a>';
      str[p++] = '<input type="text" value="'+l.title+'" style="display:none" onblur="changeInputValue(this,'+l.id+',2)"/>';
      str[p++] = '</span></li>';
    }
  }
  str[p++] = '</ul>';
  return str.join('');
}

/*  画点击收藏按钮弹出层  */
function _getHTMLFavFile(data){
  var str = [], p = 0;
  //str[p++] = '<div onclick="javascript:alert(添加到收藏夹)" icon="icon-save-add">添加到收藏夹</div><div class="menu-sep"></div>';
  for (var i in data) {
    var d = data[i];
    str[p++] = '<div onclick="clickFavFlie('+d.id+')">';
    str[p++] = '<span class="icon-document2">'+d.title+'</span>';
    str[p++] = '<div style="width:180px;">';
    for (var j in d.downLine) {
      var dj = d.downLine[j];
      str[p++] = '<div class="icon-document2" onclick="clickFavFlie('+dj.id+')">';
      str[p++] = '<span>'+dj.title+'</span>';
      str[p++] = '<div style="width:180px;">';
      for (var k in dj.downLine) {
        var dk = dj.downLine[k];
        str[p++] = '<div icon="icon-star3" onclick="clickFavFlie('+dk.id+')">'+dk.title+'</div>';
      }
      str[p++] = '<div icon="icon-save2" onclick="clickNewFavFile('+dj.id+',3)">新建文件夹</div>';
      str[p++] = '</div></div>';
    }
    str[p++] = '<div icon="icon-save2" onclick="clickNewFavFile('+d.id+',2)">新建文件夹</div>';
    str[p++] = '</div></div>';
  }
  str[p++] = '<div icon="icon-save2" onclick="clickNewFavFile(0,1)">新建文件夹</div>';
  str[p++] = '<div class="menu-sep"></div>';
  return str.join('');
}

/*  点击收藏弹出层，再点击每个文件夹的触发事件，用来保存文章到对应的收藏夹  */
function clickFavFlie(fid){
  if (!fid || fid <= 0) {
    alert('保存出错');
    window.location.href = url;
    return ;
  }
  var str = window.prompt("请输入收藏名称",cinfo['title']);
  if (str) cinfo['title'] = str;
  var data = {
    type:nid,
    tid:cinfo['id'],
    title:cinfo['title'],
    fid:fid
  };
  ajax.send(url+"/ajaxSaveFavContent",data,function(obj){
    initCollect(obj.data);
    //$("#collect").click();
  });
}

/*  在文件夹和文件点击右键的处理事件
 *  以下函数可以改进，暂时没空
 *  params  e:      右键点击事件
 *  params  self:   this
 *  params  id:     改名/删除对应的id
 *  params  type:   1-文件夹   2-文章
 *  params  level:  新建文件夹对应的level，1/2/3对应建立文件夹的level，4-弹出提示说明最多建立三级目录，9-隐藏“新建文件夹”
 *  */
function rightclickMenu(e,self,id,type,level){
  var o = ["divNewFile",'divRepeat','divDelete'];
  for (var i in o) {
    $("#"+o[i]).unbind('click').show();
  }
  if (level == 1) {
    $("#divRepeat").hide();
    $("#divDelete").hide();
  }else if (level == 9) $("#divNewFile").hide();
  $('#divRightClick').menu();
  var mosuePos = getMousePos(e);
  $('#divRightClick').menu('show',{
    left: mosuePos.x,
    top: mosuePos.y
  });
  $("#divNewFile").click(function(){
    newFavFlie(id,level);
  });
  $("#divRepeat").click(function(){
    dbclickChangeName(self);
    $("#divRepeat").unbind();
  });
  $("#divDelete").click(function(){
    deleteFavFile(id,type);
  });
}

//获取鼠标位置，兼容IE,FF,Chrome
function getMousePos(ev){
  ev = ev || window.event;
  var mousePos = {
    x:0,
    y:0
  };
  if(ev.pageX || ev.pageY) mousePos = {
    x:ev.pageX,
    y:ev.pageY
  };
  else mousePos = { 
    x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
    y:ev.clientY + document.body.scrollTop   - document.body.clientTop
  };
  return mousePos;
}

function newFavFlie(id,level){
  if (level == 4) {
    alert("最多只能建立三级目录！");
    return ;
  }
  var str = window.prompt("请输文件夹名称","新建文件夹")
  if (str == null) return ;
  if (str == "") {
    alert('输入的文件夹名称不能为空');
    return ;
  }
  var data = {
    type:nid,
    id:id,
    level:level,
    uid:uid,
    title:str
  };
  ajax.send(url+"/newFavFile",data,function(obj){
    initCollect(obj.data);
  });
}

/*  右键->删除  操作
 *  id:     删除记录的id
 *  type:   1-删除文件夹，2-删除文章
 *  */
function deleteFavFile(id,type){
  var data = {
    id:id,
    type:type,
    nid:nid
  };
  ajax.send(url+"/ajaxDeleteFavList",data,function(obj){
    initCollect(obj.data);
  })
}

function initCollect(data){
  replaceHtml("divFavList", _getHTMLFavList(data));
  $("#tt1").tree();
  setTab1(2,1);
  replaceHtml("mm", _getHTMLFavFile(data));
  $('#mm').menu();
  $('#collect').unbind('click').click(function(){
    $('#mm').menu('show',showPos);
  });
  $("#lnkHiddent").click();
}